package com.gzyz.drug.tencentapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gzyz.drug.entity.ChinaTotal;
import com.gzyz.drug.entity.NocvData;
import com.gzyz.drug.entity.TianJin;
import com.gzyz.drug.service.ChinaTotalService;
import com.gzyz.drug.service.IndexService;
import com.gzyz.drug.service.TJService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ChinaTotalScheduleTask {


    /*@Autowired
    private ChinaTotalService chinaTotalService;
    @Autowired
    private IndexService indexService;
    @Autowired
    private TJService tjService;

    *//**
     * 每小时执行一次，更新全国情况
     * @throws Exception
     *//*
    @Scheduled(fixedDelay = 10000000)  //1000s执行一次
//    @Scheduled(cron="0 0 8,9,10,11,12,13,18,20 * * ?")
    public void updateChinaTotalToDB() throws Exception {
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
//        System.out.println("原始数据："+ string);

        //将字符串转换成为jsonobject格式，拿到里面的key-value
        //1.所有数据的alibaba格式
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");
//        System.out.println("data"+ data);

        //2.拿到data类型的alibaba数据
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");
//        System.out.println("chinaTotal"+chinaTotal);

        //3.解析 total  全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");
//        System.out.println("total"+total);

        //4. 全国数据 total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");
//        System.out.println("confirm"+confirm);

        //5.为程序实体赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setInput((Integer) input);
        dataEntity.setSevere((Integer) severe);
        dataEntity.setHeal((Integer) heal);
        dataEntity.setDead((Integer) dead);
        dataEntity.setSuspect((Integer) suspect);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dataEntity.setUpdateTime(format.parse(String.valueOf(lastUpdateTime)));

        //6.插入数据库(将最新数据返回到页面，历史数据依旧保存到数据库留存备份)
        chinaTotalService.save(dataEntity);





        *//**
         * 中国各个省份数据的自动刷新
         *//*
        //拿到AreaTree(各个国家的数据)
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
        Object[] objects = areaTree.toArray();
        //遍历所有的国家
        *//*for (int i = 0; i < objects.length; i++) {
            JSONObject jsonObject1 = JSONObject.parseObject(objects[i].toString());
            Object name = jsonObject1.get("name");
            System.out.println(name);
        }*//*

        //拿到中国的数据
        JSONObject jsonObject1 = JSONObject.parseObject(objects[2].toString());
        JSONArray children = jsonObject1.getJSONArray("children");//各个省份的json数据
        Object[] objects1 = children.toArray();//各个省份

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<NocvData> list = new ArrayList<>();

        for (int i = 0; i < objects1.length; i++) {
            NocvData nocvData = new NocvData();

            JSONObject jsonObject2 = JSONObject.parseObject(objects1[i].toString());
//            System.out.println(jsonObject2);
            Object name = jsonObject2.get("name");  //得到省份名字
            List<TianJin> list1 = new ArrayList<>();
            if (name.equals("天津")) {
                JSONArray children2 = jsonObject2.getJSONArray("children");
                Object[] objects3 = children2.toArray();
                for (int n = 0; n < objects3.length; n++) {
                    TianJin t = new TianJin();
                    JSONObject jsonObject3 = JSONObject.parseObject(objects3[n].toString());
//                    System.out.println(children2);
//                    System.out.println("-------------------------");
//                    System.out.println(objects3);
//                    System.out.println("-------------------------");
//                    System.out.println(jsonObject3);
//                    System.out.println("-------------------------");
                    Object name2 = jsonObject3.get("name");  //得到区的名字
//                    System.out.println(name);
                    Object timePro2 = jsonObject3.get("lastUpdateTime");  //得到区更新数据时间
                    Object total2 = jsonObject3.get("total");
                    JSONObject jsonObject4 = JSONObject.parseObject(total2.toString());//拿到total
                    Object confirm2 = jsonObject4.get("confirm");//拿到累计确诊数量
                    Object heal2 = jsonObject4.get("heal");//拿到累计治愈数量
                    Object dead2 = jsonObject4.get("dead");//拿到累计死亡数量
                    int xianConfirm2 = Integer.parseInt(confirm2.toString()) - Integer.parseInt(heal2.toString()) - Integer.parseInt(dead2.toString());
                    int confirmed = Integer.parseInt(confirm2.toString());
//            nocvData.setValue(Integer.parseInt(confirm1.toString()));//累计确诊人数
                    if (xianConfirm2 != 0) {
                        t.setName(name2.toString());
                        t.setValue(confirmed);//共计确诊人数
                        t.setUpdateTime(format1.parse(String.valueOf(timePro2)));
                        list1.add(t);
                    }

//                    System.out.println(list1);
                }
            }
            tjService.saveBatch(list1);

            Object timePro = jsonObject2.get("lastUpdateTime");  //得到省份更新数据时间
            Object total1 = jsonObject2.get("total");
            JSONObject jsonObject3 = JSONObject.parseObject(total1.toString());//拿到total
            Object confirm1 = jsonObject3.get("confirm");//拿到累计确诊数量
            Object heal1 = jsonObject3.get("heal");//拿到累计治愈数量
            Object dead1 = jsonObject3.get("dead");//拿到累计死亡数量
            //计算现存人数  = 累计确诊-治愈人数-死亡人数
            int xianConfirm = Integer.parseInt(confirm1.toString()) - Integer.parseInt(heal1.toString())-Integer.parseInt(dead1.toString());


//            System.out.println("省份-》"+name+"确诊人数"+confirm1);

            //赋值
            nocvData.setName(name.toString());
//            nocvData.setValue(Integer.parseInt(confirm1.toString()));//累计确诊人数

            nocvData.setValue(xianConfirm);//现存确诊人数
            if (timePro == null){
                nocvData.setUpdateTime(new Date());
            }else {
                nocvData.setUpdateTime(format1.parse(String.valueOf(timePro)));
            }

            list.add(nocvData);
        }

        //各个省份的数据 插入数据库  批量插入saveBatch
        indexService.saveBatch(list);


        //7. 删除缓存【非常重要】--mysql  ---redis 一致性的一个简单套路
        Jedis jedis = new Jedis("127.0.0.1");
        if (jedis != null) {
            jedis.flushDB();//注意： 这里是删除全部缓存，若有需要，只删除自己使用的就好，该删除什么删除什么
        }
    }
*/
}
